package com.lmaos.basetools.sText;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import com.lmaos.basetools.base.SBaseUtils;

public class FileUtils {
	
	public static boolean write(File file,String text,String charsetName){
		if(!file.canWrite()||!file.exists()){
			return false;
		}
		FileOutputStream out = null;
		try {
			out = new FileOutputStream(file);
			out.write(text.getBytes(Charset.forName(charsetName)));
			out.flush();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}finally{
			SBaseUtils.close(out);
		}
	}
	public static String readString(File file,String charsetName){
		if(!file.canRead()||!file.exists()){
			return null;
		}
		FileInputStream in = null;
		try {
			in = new FileInputStream(file);
			byte[] buf = new byte[in.available()];
			in.read(buf);
			return new String(buf,Charset.forName(charsetName));
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}finally{
			SBaseUtils.close(in);;
		}
	}
	
	public static List<File> getFileAll(String path){
		return getFileAll(new File(path));
	}
	public static List<File> getFileAll(File file){
		List<File> files = new ArrayList<File>();
		List<File> paths = new ArrayList<File>();
		if(file.exists()&&file.isDirectory()){
			paths.add(file);
			files.add(file);
			for (int i = 0; i < paths.size(); i++) {
				File cfile = paths.get(i);
				File[] cfiles = cfile.listFiles();
				if(cfiles!=null){
					for (File ccfile : cfiles) {
						if(ccfile.isDirectory()){
							paths.add(ccfile);
						}
						files.add(ccfile);
					}
				}
			}
		}
		return files;
	}
	public static List<File> findFileBySuffix(String path,String... suffixs){
		
		List<File> files = new ArrayList<File>();
		List<File> paths = new ArrayList<File>();
		File file = new File(path);
		if(file.exists()&&file.isDirectory()){
			paths.add(file);
			for (int i = 0; i < paths.size(); i++) {
				File cfile = paths.get(i);
				File[] cfiles = cfile.listFiles();
				if(cfiles!=null)
				for (File ccfile : cfiles) {
					if(ccfile.isDirectory()){
						paths.add(ccfile);
					}else if(suffixs==null||suffixs.length==0){
						files.add(ccfile);
					}else{
						for (String suffix : suffixs) {
							if(!TextUtils.isBlank(suffix)&&ccfile.getName().endsWith(suffix)){
								files.add(ccfile);
							}
						}
					}
				}
			}
		}
		return files;
	}
	
	public static boolean copy(File srcFile,File desFile){
		
		if(srcFile==null||desFile==null||srcFile.isDirectory()||!srcFile.exists()||desFile.isDirectory()){
			
			return false;
		}
		if(!desFile.getParentFile().exists()){
			desFile.mkdirs();
		}
		
		FileInputStream in = null;
		FileOutputStream out = null;
		
		try {
			if(!desFile.exists()){
				desFile.createNewFile();
			}
			in = new FileInputStream(srcFile);
			out= new FileOutputStream(desFile);
			int v = 0;
			while((v=in.read())!=-1){
				out.write(v);
			}
			out.flush();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}finally{
			SBaseUtils.close(out);
			SBaseUtils.close(in);
		}
	}
	public static void main(String[] args) {
		System.out.println(findFileBySuffix("/Users/zhangxingyu/project/ex/", ".java",".jar").size());
	}
}
